package org.tgcloud.group.service.manager.process;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.entity.MjBusinessPerson;
import org.tgcloud.group.entity.MjNormalUser;
import org.tgcloud.group.entity.ProjectProcess;
import org.tgcloud.group.entity.SysManagerUser;
import org.tgcloud.group.repository.*;
import org.tgcloud.group.web.controller.dto.process.ProcessToUser;
import org.tgcloud.group.web.controller.manager.process.enums.ProcessEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhy
 * @Created: 2025-08-19-上午10:58
 * @Description:
 */
@Service
public class ProcessManageService {

    @Autowired
    private ProjectProcessMapper processMapper;

    @Autowired
    private SysManagerUserMapper managerUserMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    @Autowired
    private MjNormalUserMapper normalUserMapper;

    @Autowired
    private ProjectProcessReadMapper processReadMapper;

    public void doAddProcess(Integer projectId, Integer userId, Short userType, ProcessEnum processEnum, List<ProcessToUser> toUserList){
        ProjectProcess process = new ProjectProcess();
        process.setProjectId(projectId);
        process.setUserType(userType);
        process.setUserId(userId);
        process.setCreateTime(new Date());
        process.setStatus((short) 1);
        process.setProcess(String.format(processEnum.getValue(), getUserName(userId, userType)));
        process.setProcessType(processEnum.getType());
        processMapper.insertSelective(process);
        doAddProcessUser(toUserList, process.getId());
    }

    public String getUserName(Integer userId, Short userType){
        String username = "";
        if (userType == 3){
            MjNormalUser user = normalUserMapper.selectByPrimaryKey(userId);
            if (user != null){
                username = user.getNickName();
            }
        } else if (userType == 2){
            MjBusinessPerson user = businessPersonMapper.selectByPrimaryKey(userId);
            if (user != null){
                username = user.getUsername();
            }
        } else if (userType == 1){
            SysManagerUser user = managerUserMapper.selectByPrimaryKey(userId);
            if (user != null){
                username = user.getUsername();
            }
        }
        return username;
    }

    public void doAddProcessUser(List<ProcessToUser> toUserList, Integer processId){
        if (toUserList == null || toUserList.isEmpty() || processId == null){
        }else {
            processReadMapper.insertBatch(toUserList, processId);
            List<String> collect = toUserList.stream().map(item -> item.getUserType() + "-" + item.getUserId()).collect(Collectors.toList());
            String processReadRedisKey = "process:";
            for (String key: collect){
                String redisKey = processReadRedisKey + key;
                List<Integer> cacheList = RedisUtil.getCacheList(redisKey);
                if (cacheList == null || cacheList.isEmpty()){
                    List<Integer> list = new ArrayList<>();
                    list.add(processId);
                    RedisUtil.setCacheList(redisKey, list);
                }else {
                    cacheList.add(processId);
                    RedisUtil.setCacheList(redisKey, cacheList);
                }
            }
        }
    }

}
